package cn.com.ghy.android.widget.plan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.VisitPlanBean;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.R;

public class CurVisitPlan extends Activity {

	private TextView labTxtMonth = null;
	private TextView labTxtVisitPlanStatus = null;
	private ListView listVisitPlanList = null;
	private Bundle bundle = null;
	private ImageButton btnBack = null;
	private ImageButton btnList = null;

	private View appToolBar = null;

	private String visitPlanStatus = "";
	private List<VisitPlanBean> visitPlanList = null;

	private String monthid = null;
	private Map<String, String> visitPlanStatusMap = null;

	private VisitPlanListAdapter visitPlanListAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cur_visit_plan);
		getSession();
		setToolBar();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		bundle = this.getIntent().getExtras();
		visitPlanStatus = bundle.getString("curPlanStatus");
		visitPlanList = (List<VisitPlanBean>) bundle.getSerializable("curPlanLast");
		monthid = bundle.getString("curMonthid");
		if (null == visitPlanStatus || "".equals(visitPlanStatus)) {
			Toast.makeText(CurVisitPlan.this, "程序出错,请与管理员联系!", Toast.LENGTH_LONG).show();
			CurVisitPlan.this.finish();
		}
		if (null == monthid || "".equals(monthid)) {
			Toast.makeText(CurVisitPlan.this, "程序出错,请与管理员联系!", Toast.LENGTH_LONG).show();
			CurVisitPlan.this.finish();
		}

		visitPlanStatusMap = new HashMap<String, String>();
		visitPlanStatusMap.put(CommonStatic.visitPlan_Y, "通过审核");
		visitPlanStatusMap.put(CommonStatic.visitPlan_X, "未通过审核");
		visitPlanStatusMap.put(CommonStatic.visitPlan_A, "正在审核中");
		visitPlanStatusMap.put(CommonStatic.visitPlan_W, "保存状态");
		visitPlanStatusMap.put(CommonStatic.visitPlan_N, "未填写计划");
	}

	private void setToolBar() {
		appToolBar = findViewById(R.id.appToolBar);
		btnList = (ImageButton) appToolBar.findViewById(R.id.btnList);
		btnList.setVisibility(View.GONE);
		btnBack = (ImageButton) appToolBar.findViewById(R.id.btnBack);
	}

	private void findView() {
		appToolBar = (RelativeLayout) findViewById(R.id.appToolBar);

		labTxtMonth = (TextView) this.findViewById(R.id.labTxtMonth);
		labTxtVisitPlanStatus = (TextView) this.findViewById(R.id.labTxtVisitPlanStatus);

		listVisitPlanList = (ListView) this.findViewById(R.id.listVisitPlanList);
	}

	private void init() {
		visitPlanListAdapter = new VisitPlanListAdapter(CurVisitPlan.this, visitPlanList);
		listVisitPlanList.setAdapter(visitPlanListAdapter);
		labTxtMonth.setText(monthid);
		labTxtVisitPlanStatus.setText(visitPlanStatusMap.get(visitPlanStatus));
	}

	public void addListener() {
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				CurVisitPlan.this.finish();
			}
		});
	}

	private class VisitPlanListAdapter extends BaseAdapter {

		private Context context = null;
		private List<VisitPlanBean> list = null;

		public VisitPlanListAdapter(Context context, List<VisitPlanBean> list) {
			this.context = context;
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public VisitPlanBean getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
					R.layout.visit_plan_lay, null);

			VisitPlanBean bean = list.get(position);
			TextView tv = (TextView) linearLayout.findViewById(R.id.labTxtDay);
			tv.setText(bean.getVsdate() + "");
			tv = (TextView) linearLayout.findViewById(R.id.labTxtWeek);
			tv.setText(bean.getWk());
			tv = (TextView) linearLayout.findViewById(R.id.labTxtWay);
			tv.setText(null == bean.getMyWayBean().getWayname() ? "" : bean.getMyWayBean()
					.getWayname());
			tv = (TextView) linearLayout.findViewById(R.id.labTxtWorkType);
			tv.setText(null == bean.getVisitPlanWorkTypeBean().getWorkTypeName() ? "" : bean
					.getVisitPlanWorkTypeBean().getWorkTypeName());

			return linearLayout;
		}
	}
}
